package com.ljx.itbbs.server.mq.config;

import com.ljx.itbbs.server.mall.consumer.OrderExpireConsumer;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: LiJunXi
 * @Description: 订单超时死信队列
 * @Date: 2024-07-06  17:06
 */
@Configuration
public class DlxMqConfig {

    private final static String DEAD_ORDER_EXCHANGE = "itbbs.dead.exchange";
    private final static String DEAD_ORDER_QUEUE = "itbbs.dead.queue";
    public final static String ORDER_EXCHANGE = "itbbs.order.exchange";
    public final static String ORDER_QUEUE = "itbbs.order.queue";

    // 15分钟
    private final static int DEAD_ORDER_EXPIRE_TIME = 100 * 60;

    @Bean
    public Queue orderQueue(){
        Map<String, Object> args = new HashMap<>(2);
        // 绑定我们的死信交换机
        args.put("x-dead-letter-exchange", DEAD_ORDER_EXCHANGE);
        // 绑定我们的路由key
        args.put("x-dead-letter-routing-key", DEAD_ORDER_QUEUE);
        // 声明队列的TTL
        args.put("x-message-ttl", DEAD_ORDER_EXPIRE_TIME);
        return new Queue(ORDER_QUEUE, true, false, false, args);
    }

    //绑定死信队列
    @Bean
    public Queue dlxQueue() {
        return new Queue(DEAD_ORDER_QUEUE);
    }
    //绑定死信交换机
    @Bean
    public DirectExchange dlxExchange() {
        return new DirectExchange(DEAD_ORDER_EXCHANGE);
    }

    @Bean
    public DirectExchange orderExchange(){
        return new DirectExchange(ORDER_EXCHANGE);
    }

    @Bean
    public Binding orderBindingExchange() {
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with(ORDER_QUEUE);
    }

    @Bean
    public Binding deadBindingExchange() {
        return BindingBuilder.bind(dlxQueue()).to(dlxExchange()).with(DEAD_ORDER_QUEUE);
    }

    /**
     * 注入订单监听器
     */
    @Autowired
    private OrderExpireConsumer orderExpireConsumer;

    /**
     * 监听器容器配置
     */
    @Autowired
    private SimpleRabbitListenerContainerFactoryConfigurer factoryConfigurer;

    /**
     * channel链接工厂
     */
    @Autowired
    private CachingConnectionFactory connectionFactory;

    /**
     * 订单队列监听器容器
     * @return
     */
    @Bean
    public SimpleMessageListenerContainer orderExpireConsumerContainer(){
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factoryConfigurer.configure(factory, connectionFactory);
        SimpleMessageListenerContainer listenerContainer = factory.createListenerContainer();
        listenerContainer.setMessageListener(orderExpireConsumer);
        listenerContainer.setQueues(dlxQueue());
        return listenerContainer;
    }

}
